package io.rehuo.modules.app.utils;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.i18n.phonenumbers.PhoneNumberToCarrierMapper;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
import com.google.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder;
import io.rehuo.common.utils.HttpUtil;
import io.rehuo.common.utils.http.HttpUtils;
import org.apache.commons.lang.StringUtils;

public class PhoneUtil {


    private static PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();

    private static PhoneNumberToCarrierMapper carrierMapper = PhoneNumberToCarrierMapper.getInstance();

    private static PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance();

    /**
     * 根据国家代码和手机号  判断手机号是否有效
     *
     * @param phoneNumber
     * @param countryCode
     * @return
     */
    public static boolean checkPhoneNumber(String phoneNumber, String countryCode) {
        int ccode = Integer.valueOf(countryCode);
        long phone = Long.valueOf(phoneNumber);
        Phonenumber.PhoneNumber pn = new Phonenumber.PhoneNumber();
        pn.setCountryCode(ccode);
        pn.setNationalNumber(phone);
        return phoneNumberUtil.isValidNumber(pn);

    }

    /**
     * 根据国家代码和手机号  判断手机运营商
     *
     * @param phoneNumber
     * @param countryCode
     * @return
     */
    public static String getCarrier(String phoneNumber, String countryCode) {
        int ccode = Integer.valueOf(countryCode);
        long phone = Long.valueOf(phoneNumber);
        Phonenumber.PhoneNumber pn = new Phonenumber.PhoneNumber();
        pn.setCountryCode(ccode);
        pn.setNationalNumber(phone);
        //返回结果只有英文，自己转成成中文
        String carrierEn = carrierMapper.getNameForNumber(pn, Locale.ENGLISH);
        String carrierZh = "";
        carrierZh += geocoder.getDescriptionForNumber(pn, Locale.CHINESE);
        switch (carrierEn) {
            case "China Mobile":
                carrierZh += "移动";
                break;
            case "China Unicom":
                carrierZh += "联通";
                break;
            case "China Telecom":
                carrierZh += "电信";
                break;
            default:
                break;
        }
        return carrierZh;
    }


    /**
     * @param @param  phoneNumber
     * @param @param  countryCode
     * @param @return 参数
     * @throws
     * @Description: 根据国家代码和手机号  手机归属地
     */
    public static String getGeo(String phoneNumber, String countryCode) {
        int ccode = Integer.valueOf(countryCode);
        long phone = Long.valueOf(phoneNumber);
        Phonenumber.PhoneNumber pn = new Phonenumber.PhoneNumber();
        pn.setCountryCode(ccode);
        pn.setNationalNumber(phone);
        return geocoder.getDescriptionForNumber(pn, Locale.CHINESE);
    }


    /**
     * @return String    返回类型
     * @throws
     * @Title: getPhoneRegionCode
     * @Description: 得到手机的归宿地编码
     */
    public static String getPhoneRegionCode(String phoneNumber, String countryCode) {
        String areaName = getGeo(phoneNumber, countryCode);
        if (StringUtils.isEmpty(areaName)) {
            return "";
        }
        if (areaName.length() < 3) {
            return "";
        }
        return areaName;
    }

    //也可以通过淘宝在线获取
    public static String loadJSON (String phone) {
        String url = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm";
//        Map map = new HashMap();
//        map.put("tel",phone);
        String param = "tel=" + phone;
        try {
            String res = HttpUtils.sendGet(url,param);
            JSONObject jsonObject = (JSONObject) JSON.parse(res.substring(res.indexOf("{"),res.length()));
            return String.valueOf(jsonObject.get("province"));
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }



    public static void main(String[] args) {
        System.out.println(getPhoneRegionCode("18932420290", "86"));
//        System.out.println(loadJSON("18932420290"));
    }
}